﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace BlogDH
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    if (!string.IsNullOrEmpty(this.TextBox1.Text.Trim()) && this.TextBox2.Text == "g8816886")
            //    {
            //        int count = DAL.BlogInfo.Update(this.TextBox1.Text);

            //        Response.Write(count.ToString());
            //    }
            //}
            //catch (Exception ex)
            //{ Response.Write(ex.ToString()); }


            List<Model.UserInfo> list = new BLL.UserInfo().GetModelList("");

            for (int i = 0; i < list.Count; i++)
            {



                string sql = string.Format("update bloginfo set userid={0} where blogurl='{1}'", list[i].UserID, list[i].UserName);
                DAL.BlogInfo.Update(sql).ToString();
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            StringBuilder s = new StringBuilder();
            if (this.TextBox2.Text == "g8816886")
            {
                int[] aids = new int[] { 11, 12, 13, 14, 15, 21, 22, 23, 31, 32, 33, 34, 35, 36, 37, 41, 42, 43, 44, 45, 46, 50, 51, 52, 53, 54, 61, 62, 63, 64, 65, 71, 81, 82 };
                string sql;
                for (int i = 0; i < aids.Length; i++)
                {
                    sql = string.Format("update bloginfo set state=1 where blogid in (select top 10 blogid from bloginfo where sheng={0} and state=2)", aids[i]);
                    s.AppendFormat("{0}:{1}<br/>", aids[i], DAL.BlogInfo.Update(sql));
                }
            }

            Response.Write(s.ToString());
        }


        protected void Button3_Click1(object sender, EventArgs e)
        {
            if (this.TextBox2.Text != "g8816886")
                return;
            BLL.BlogInfo bll = new BLL.BlogInfo();
            List<Model.BlogInfo> list = bll.GetModelList("state <>0 and gocount=1");

            for (int i = 0; i < list.Count; i++)
            {
                string strHtml = GetHttpWebRequest(string.Format("http://www.{0}", list[i].BlogUrl));

                if (string.IsNullOrEmpty(strHtml))
                {
                    list[i].State = 0;
                }

                list[i].GoCount = 2;
                bll.Update2(list[i]);
            }

            Response.Write("Over" + list.Count);
        }
        public static string GetHttpWebRequest(string url)
        {
            string strHTML = string.Empty;
            try
            {
                Uri uri = new Uri(url);
                HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(uri);
                myReq.UserAgent = "User-Agent:Mozilla/4.2 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705";
                myReq.Accept = "*/*";
                //myReq.Referer = "www.google.com";
                //myReq.KeepAlive = true;
                myReq.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
                HttpWebResponse result = (HttpWebResponse)myReq.GetResponse();
                Stream receviceStream = result.GetResponseStream();
                StreamReader readerOfStream = new StreamReader(receviceStream, System.Text.Encoding.GetEncoding("gb2312"));
                strHTML = readerOfStream.ReadToEnd();
                readerOfStream.Close();
                receviceStream.Close();
                result.Close();
            }
            catch (Exception)
            {
            }
            return strHTML;
        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
            string count = "error";
            if (!string.IsNullOrEmpty(this.TextBox1.Text.Trim()) && this.TextBox2.Text == "g8816886")
            {
                string sql = string.Format("update bloginfo set state=2 where blogurl='{0}'", this.TextBox1.Text.Trim());
                count = DAL.BlogInfo.Update(sql).ToString();
            }
            Response.Write(count);
        }
    }
}